package com.nanhai.buddhist.college.module.study.service.studysubject;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studysubject.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studysubject.StudySubjectDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studysubject.StudySubjectConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studysubject.StudySubjectMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 课程管理 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class StudySubjectServiceImpl implements StudySubjectService {

    @Resource
    private StudySubjectMapper subjectMapper;

    @Override
    public String createSubject(StudySubjectCreateReqVO createReqVO) {
        // 插入
        StudySubjectDO subject = StudySubjectConvert.INSTANCE.convert(createReqVO);
        subjectMapper.insert(subject);
        // 返回
        return subject.getId();
    }

    @Override
    public void updateSubject(StudySubjectUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSubjectExists(updateReqVO.getId());
        // 更新
        StudySubjectDO updateObj = StudySubjectConvert.INSTANCE.convert(updateReqVO);
        subjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubject(String id) {
        // 校验存在
        this.validateSubjectExists(id);
        // 删除
        subjectMapper.deleteById(id);
    }

    private void validateSubjectExists(String id) {
        if (subjectMapper.selectById(id) == null) {
            throw exception(SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public StudySubjectDO getSubject(String id) {
        return subjectMapper.selectById(id);
    }

    @Override
    public List<StudySubjectDO> getSubjectList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : subjectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudySubjectDO> getSubjectPage(StudySubjectPageReqVO pageReqVO) {
        return subjectMapper.selectPage0(pageReqVO);
    }

    @Override
    public List<StudySubjectDO> getSubjectList(StudySubjectExportReqVO exportReqVO) {
        return subjectMapper.selectList(exportReqVO);
    }

    @Override
    public List<StudySubjectDO> getStudySubjectDOListByStr(String findStr) {
        LambdaQueryWrapper<StudySubjectDO> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(findStr)){
            wrapper.like(StudySubjectDO::getName, findStr);
        }
        return subjectMapper.selectList(wrapper);
    }

}
